<template>
  <div class="detail">
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="nav"></detail-nav-bar>
    <scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll">
      <detail-swiper v-bind:top-images="topImages"></detail-swiper>
      <detail-base-info v-bind:goods="goods"></detail-base-info>
      <detail-shop-info v-bind:shop="shop"></detail-shop-info>
      <detail-goods-info v-bind:detail-info="detailInfo" @imageLoad="imageLoad"></detail-goods-info>
      <detail-param-info v-bind:param-info="paramInfo" ref="params"></detail-param-info>
      <detail-comment-info v-bind:comment-info="commentInfo" ref="comment"></detail-comment-info>
      <goods-list v-bind:goods="recommends" ref="recommend"></goods-list>
    </scroll>
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
<!--    <toast :message="message" :show="show"></toast>-->
    <!--    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>-->
  </div>
</template>

<script>
  import DetailNavBar from "./childComps/DetailNavBar";
  import DetailSwiper from "./childComps/DetailSwiper";
  import DetailBaseInfo from "./childComps/DetailBaseInfo";
  import DetailShopInfo from "./childComps/DetailShopInfo";
  import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
  import Scroll from "../../components/common/scroll/Scroll";
  import DetailParamInfo from "./childComps/DetailParamInfo";
  import DetailCommentInfo from "./childComps/DetailCommentInfo";
  import GoodsList from "../../components/content/goods/GoodsList";
  import DetailBottomBar from "./childComps/DetailBottomBar";
  import BackTop from "../../components/content/backTop/BackTop";
  // import Toast from "../../components/common/toast/Toast";


  import {getDetail, getRecommend, Goods, Shop, GoodsParam} from "../../network/detail";
  import {itemListenerMixin, backTopMixin} from "../../common/minxin";
  import {debounce} from "../../common/utils";

  export default {
    name: "Detail",
    components: {
      DetailNavBar,
      DetailSwiper,
      DetailBaseInfo,
      DetailShopInfo,
      DetailGoodsInfo,
      DetailParamInfo,
      DetailCommentInfo,
      DetailBottomBar,
      // Toast,
      GoodsList,
      BackTop,
      Scroll,
    },
    mixins: [itemListenerMixin, backTopMixin],
    data() {
      return {
        iid: null,
        topImages: [],
        goods: {},
        shop: {},
        detailInfo: {},
        paramInfo: {},
        commentInfo: {},
        recommends: [],
        themeTopYs: [],
        getThemeTopY: null,
        currentIndex: 0,
        // message:'',
        // show:false
        // isShowBackTop:false,
      }
    },
    methods: {
      imageLoad() {
        this.$refs.scroll.refresh();
        this.getThemeTopY()
      },
      titleClick(index) {
        this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 500)
      },
      contentScroll(position) {
        //1.获取Y值
        // console.log(position)
        const positionY = -position.y
        //  2.positionY 和主题中的进行对比决定显示哪个Index
        let length = this.themeTopYs.length
        for (let i = 0; i < this.themeTopYs.length; i++) {
          if (this.currentIndex !== i && ((i < length - 1 && positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1]) || (i === length - 1 && positionY >= this.themeTopYs[i]))) {
            this.currentIndex = i;
            // console.log(this.currentIndex)
            this.$refs.nav.currentIndex = this.currentIndex
          }
        }

        // 3.是否显示回到顶部
        this.isShowBackTop = (-position.y) > 1000

      },
      addToCart() {
        //1.获取购物车需要展示的信息
        const product = {}
        product.image = this.topImages[0];
        product.title = this.goods.title;
        product.desc = this.detailInfo.desc;
        product.price = this.goods.nowPrice;
        product.iid = this.iid;

        // 2.将商品添加到购物车中
        // this.$store.commit('addCart',product)
        this.$store.dispatch('addCart', product).then(res => {
          //3.show toast添加到购物车成功
          // console.log(res)
          // this.show = true;
          // this.message = res;
          // setTimeout(()=>{
          //   this.show = false;
          //   this.message = ''
          // },2000)

          this.$toast.show(res,2000)
        })
      },
      // backClick(){
      //   this.$refs.scroll.s'/detailcrollTo(0,0)
      // },
    },
    created() {
      // 1.保存传入的iid
      this.iid = this.$route.params.iid
      // 2.根据iid请求详情数据
      getDetail(this.iid).then(res => {
        // 2.1.获取结果
        const data = res.data.result;
        // 2.2.获取顶部信息
        this.topImages = data.itemInfo.topImages;
        // 2.3.获取商品信息
        this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services);
        // 2.4.获取店铺信息
        this.shop = new Shop(data.shopInfo);
        // 2.5.获取商品信息
        this.detailInfo = data.detailInfo
        // 2.6.保存参数信息
        this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule);
        // 2.7.保存评论信息
        if (data.rate.list) {
          this.commentInfo = data.rate.list[0];
        }
        //给getThemeTopY赋值（对给this.themeTopYS赋值的操作进行防抖）
        this.getThemeTopY = debounce(() => {

          this.themeTopYs = [];
          this.themeTopYs.push(0);
          this.themeTopYs.push(this.$refs.params.$el.offsetTop - 44);
          this.themeTopYs.push(this.$refs.comment.$el.offsetTop - 44);
          this.themeTopYs.push(this.$refs.recommend.$el.offsetTop - 44);
        }, 500)

        // //1.第一次获取，值是不对的:图片没有加载完   $el没有渲染完
        // this.themeTopYs = [];
        // this.themeTopYs.push(0);
        // this.themeTopYs.push(this.$refs.params.$el.offsetTop - 44);
        // this.themeTopYs.push(this.$refs.comment.$el.offsetTop - 44);
        // this.themeTopYs.push(this.$refs.recommend.$el.offsetTop - 44);
        //
        //
        // this.$nextTick(()=>{
        //   //2.第二次获取，值也不对，根据最新的数据，对应的DOM已经渲染完了
        //   //但是图片依然是没有渲染完成，所以会存在跳转不准的问题（目前获取到的offset是不包含图片的高度）
        //   //一般offsettop值不对的时候，都是因为图片导致的
        //   this.themeTopYs = [];
        //   this.themeTopYs.push(0);
        //   this.themeTopYs.push(this.$refs.params.$el.offsetTop - 44);
        //   this.themeTopYs.push(this.$refs.comment.$el.offsetTop - 44);
        //   this.themeTopYs.push(this.$refs.recommend.$el.offsetTop - 44);
        // })


      })
      //3.请求推荐数据
      getRecommend().then(res => {
        this.recommends = res.data.data.list
      })
    },
    mounted() {

    },
    updated() {

    },
    destroyed() {
      this.$bus.$off('itemImgLoad', this.itemImgListener)
    }
  }
</script>

<style scoped>
  .detail {
    position: relative;
    z-index: 1000;
    background-color: #ffffff;
    height: 100vh;
  }

  .detail-nav {
    position: relative;
    z-index: 1001;
    background-color: #ffffff;
  }

  .content {
    /*height: calc(100% - 44px);*/
    position: absolute;
    top: 44px;
    bottom: 58px;
  }
</style>
